<!-- 用户管理 -->
<template>
  <div class="user-management">
    <h2>用户管理</h2>

    <!-- 用户列表 -->
    <el-table :data="paginatedUsers" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="role" label="角色"></el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button size="small" @click="editUser(row)" type="primary">编辑</el-button>
          <el-button size="small" @click="deleteUser(row.id)" type="danger">删除</el-button>
          <el-button size="small" @click="addUser" type="success">添加用户</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        layout="prev, pager, next"
        :total="users.length"
        :page-size="pageSize"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>

    <!-- 编辑/新增用户弹窗 -->
    <el-dialog :title="isEdit ? '编辑用户' : '添加用户'" v-model="editDialogVisible" width="30%">
      <el-form :model="editingUser" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="editingUser.username" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editingUser.email" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="editingUser.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="管理员" value="管理员" />
            <el-option label="普通用户" value="普通用户" />
            <el-option label="访客" value="访客" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="editingUser.status" placeholder="请选择状态" style="width: 100%">
            <el-option label="启用" value="启用" />
            <el-option label="禁用" value="禁用" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveUser">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UserManagement',
  data() {
    return {
      // 模拟用户数据
      users: [
        { id: 1, username: '张三', email: 'zhangsan@example.com', role: '管理员', status: '启用' },
        { id: 2, username: '李四', email: 'lisi@example.com', role: '普通用户', status: '启用' },
        { id: 3, username: '王五', email: 'wangwu@example.com', role: '访客', status: '禁用' }
      ],
      currentPage: 1,
      pageSize: 5,

      editDialogVisible: false,
      editingUser: {},
      isEdit: true
    };
  },
  computed: {
    paginatedUsers() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.users.slice(start, end);
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
    },

    editUser(user) {
      this.editingUser = { ...user };
      this.isEdit = true;
      this.editDialogVisible = true;
    },

    deleteUser(id) {
      this.$confirm('确定要删除该用户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.users = this.users.filter(u => u.id !== id);
        if (this.currentPage > 1 && this.users.length <= (this.currentPage - 1) * this.pageSize) {
          this.currentPage--;
        }
      }).catch(() => {});
    },

    addUser() {
      this.editingUser = {
        id: this.users.length ? Math.max(...this.users.map(u => u.id)) + 1 : 1,
        username: '',
        email: '',
        role: '',
        status: '启用'
      };
      this.isEdit = false;
      this.editDialogVisible = true;
    },

    saveUser() {
      if (this.isEdit) {
        const index = this.users.findIndex(u => u.id === this.editingUser.id);
        if (index !== -1) {
          this.users.splice(index, 1, this.editingUser);
        }
      } else {
        this.users.push(this.editingUser);
      }
      this.editDialogVisible = false;
    }
  }
};
</script>

<style scoped>
.user-management {
  padding: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>